﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace Main
{
    class Ini
    {
        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);



        [DllImport("kernel32")]
        private static extern uint GetPrivateProfileString(

string lpAppName, // points to section name

            string lpKeyName, // points to key name

            string lpDefault, // points to default string

            byte[] lpReturnedString, // points to destination buffer

            uint nSize, // size of destination buffer

            string lpFileName  // points to initialization filename

        ); 

        /// <summary> 
        /// 写入INI文件 
        /// </summary> 
        /// <param name="Section">项目名称(如 [TypeName] )</param> 
        /// <param name="Key">键</param> 
        /// <param name="Value">值</param> 
        public static void IniWriteValue(string Section, string Key, string Value,string Path)
        {
            WritePrivateProfileString(Section, Key, Value, Path);
        }

        /// <summary> 
        /// 读出INI文件 
        /// </summary> 
        /// <param name="Section">项目名称(如 [TypeName] )</param> 
        /// <param name="Key">键</param> 
        public static string IniReadValue(string Section, string Key, string Path)
        {
            StringBuilder temp = new StringBuilder(500);
            int i = GetPrivateProfileString(Section, Key, "", temp, 500, Path);
            return temp.ToString();
        }


        /// <summary>

        /// 读取指定区域Keys列表。

        /// </summary>

        /// <param name="Section"></param>

        /// <param name="Strings"></param>

        /// <returns></returns>

        public static List<string> ReadSingleSection(string Section, string iniFilename)
        {
            List<string> result = new List<string>();
            byte[] buf = new byte[65536];
            uint lenf = GetPrivateProfileString(Section, null, null, buf, (uint)buf.Length, iniFilename);

            int j = 0;

            for (int i = 0; i < lenf; i++)

                if (buf[i] == 0)

                {

                    result.Add(Encoding.Default.GetString(buf, j, i - j));

                    j = i + 1;

                }

            return result;

        }

        /// <summary>
        /// 读取section
        /// </summary>
        /// <param name="Strings"></param>
        /// <returns></returns>
        public static List<string> ReadSections(string iniFilename)

        {

            List<string> result = new List<string>();

            byte[] buf = new byte[65536];

            uint len = GetPrivateProfileString(null, null, null, buf, (uint)buf.Length, iniFilename);

            int j = 0;

            for (int i = 0; i < len; i++)

                if (buf[i] == 0)

                {

                    result.Add(Encoding.Default.GetString(buf, j, i - j));

                    j = i + 1;

                }

            return result;

        }

        /// <summary> 
        /// 验证文件是否存在 
        /// </summary> 
        /// <returns>布尔值</returns> 
        public static bool ExistINIFile(string Path)
        {
            return File.Exists(Path);
        }
    }
}
